<?php

Yii::import('application.models._base.BaseProductCategoryRelations');


class ProductCategoryRelations extends BaseProductCategoryRelations
{
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}
		
	protected $_category_title;
	
	public function rules(){
	    $rules = array_merge(parent::rules(),array(
	        array('category_title','safe' )
	    ));
	     
	    return $rules;
	}
	
	public function findByPk($pk, $condition = '', $params = Array()){
	    $this->product_id = $pk;
	    return $this->findByAttributes(array('product_id' => $pk));
	}
	
	public function setCategory_title($categoryTitle){
	    $category = Category::model()->findByAttributes(array('title' => $categoryTitle));
	    if (!empty($category)){
	        $this->category_id = $category->id;
	    }
	    $this->_category_title = $categoryTitle;
	    return 1;
	}
	
	public function getCategory_title(){
	    if (!empty($this->_category_title)){
	        return $this->_category_title;
	    }
	    if (!empty($this->product_id)){
	        $categoryRelations = $this->with('category')->findByAttributes(array('product_id' => $this->product_id));
	        return CHtml::value($categoryRelations, 'category.title');
	    }
	    return null;
	}
	
	public function relations(){
	    return array(
	        'category' =>  array(self::BELONGS_TO,'Category','','on' => 't.category_id = Category.id'  ),
	    );
	}
}